package cn.regexp.web.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.regexp.domain.ShopOrder;
import cn.regexp.domain.ShopUser;
import cn.regexp.query.OrderQuery;
import cn.regexp.redis.service.IShopUserRedisService;
import cn.regexp.service.IOrderService;
import cn.regexp.utils.JsonResult;
import cn.regexp.web.model.OrderModel;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("orders")
public class OrderController {
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IShopUserRedisService shopUserRedisService;

    @PostMapping("/createOrder")
    public JsonResult<ShopOrder> createOrder(@RequestBody OrderModel orderModel, HttpServletRequest request) {
        String token = request.getHeader("token");
        ShopUser shopUser = shopUserRedisService.getUserByToken(token);
        if (ObjectUtil.isEmpty(shopUser)) {
            return JsonResult.noLogin();
        }
        ShopOrder order = orderService.createOrder(orderModel.getGoods(), orderModel.getConsignee(), orderModel.getType(), shopUser);
        return ObjectUtil.isNotEmpty(order) ? JsonResult.success(order) : JsonResult.defaultError();
    }

    @GetMapping("/query")
    public JsonResult<IPage<ShopOrder>> query(OrderQuery qo) {
        if (ObjectUtil.isEmpty(qo.getUserId())) {
            return JsonResult.noLogin();
        }
        IPage<ShopOrder> orders = orderService.queryOrder(qo);
        return JsonResult.success(orders);
    }

    @GetMapping("/getOrderByOrderSn")
    public JsonResult<ShopOrder> getOrderByOrderSn(String orderSn, HttpServletRequest request) {
        if (ObjectUtil.isEmpty(orderSn)) {
            return JsonResult.defaultError();
        }
        String token = request.getHeader("token");
        ShopUser shopUser = shopUserRedisService.getUserByToken(token);
        if (ObjectUtil.isEmpty(shopUser)) {
            return JsonResult.noLogin();
        }
        ShopOrder order = orderService.getOrderByOrderSn(orderSn);
        if (ObjectUtil.notEqual(order.getUserId(), shopUser.getId())) {
            return JsonResult.error(0, "非法操作！", null);
        }
        return JsonResult.success(order);
    }

    @ApiOperation(value = "取消订单")
    @PostMapping("/cancelOrder")
    public JsonResult<String> cancelOrder(@RequestBody String orderSn) {
        UpdateWrapper<ShopOrder> wrapper = new UpdateWrapper<>();
        wrapper.set("order_state", ShopOrder.ORDER_CANCEL)
                .eq("order_sn", orderSn);
        orderService.update(wrapper);
        return JsonResult.success();
    }

    @ApiOperation(value = "确认订单")
    @PostMapping("/confirmOrder")
    public JsonResult<String> confirmOrder(@RequestBody String orderSn) {
        UpdateWrapper<ShopOrder> wrapper = new UpdateWrapper<>();
        wrapper.set("order_state", ShopOrder.ORDER_NOT_EVALUATE)
                .eq("order_sn", orderSn)
                .set("receiving_time", DateUtil.date());
        orderService.update(wrapper);
        return JsonResult.success();
    }

    @ApiOperation(value = "订单评价")
    @PostMapping("/commentOrder")
    public JsonResult<String> commentOrder(@RequestBody String orderSn) {
        UpdateWrapper<ShopOrder> wrapper = new UpdateWrapper<>();
        wrapper.set("order_state", ShopOrder.ORDER_COMPLETED)
                .eq("order_sn", orderSn);
        orderService.update(wrapper);
        return JsonResult.success();
    }

}
